﻿let amount 
let parentCategoryID
let currentPage
let categoryID
let cityID
$(function () {
    amount = getAmount()
    parentCategoryID = getParentCategoryID()
    currentPage = getCurrentPage()
    categoryID = getCategoryID()
    cityID = getCityID()
})
function categoryChange(id) {
    cityID=0
    currentPage = 1
    categoryID = id
    dataLoad()
}
function parentCategoryChange(id) {
    parentCategoryID = id
    cityID = 0
    currentPage = 1
    dataLoad()
}
function amountChange(a) {
    cityID=0
    currentPage=1
    amount = a
    dataLoad()
}
function pageChange(p) {
    currentPage = p
    dataLoad()
}

function cityChange(a) {
    currentPage = 1
    cityID = a
    let url = `page=${currentPage}&categoryID=${categoryID}&amount=${amount}&parentCategoryID=${parentCategoryID}&cityID=${cityID}`;
    let obj ={
        page:currentPage,
        categoryID:categoryID,
        amount: amount,
        parentCategoryID: parentCategoryID,
        cityID: cityID
    }
    let jsonStr = JSON.stringify(obj)
    jsonStr = btoa(jsonStr)//base64编码
    jsonStr = encodeURIComponent(jsonStr)//URL编码
    $.ajax({
        url: '/Search/Index/' + jsonStr,
        success: function (e) {
            var tp = $(".serch-main");
            var np = $(e);
            tp.children().remove();
            np.find(".serch-main").children().appendTo($(".serch-main"));
        }
    })
    $("#citybox-div a>span").removeClass("selected")
    $("#span-city-" + cityID).addClass("selected")
}
function dataLoad() {
    let url = `page=${currentPage}&categoryID=${categoryID}&amount=${amount}&parentCategoryID=${parentCategoryID}&cityID=${cityID}`;
    let obj = {
        page: currentPage,
        categoryID: categoryID,
        amount: amount,
        parentCategoryID: parentCategoryID,
        cityID: cityID
    }
    let jsonStr = JSON.stringify(obj)
    jsonStr = btoa(jsonStr)//base64编码
    jsonStr = encodeURIComponent(jsonStr)//URL编码
    $.ajax({
        url: '/Search/Index/' + jsonStr,
        success: function (e) {
            var tp = $(".serch-content");
            var np = $(e);
            tp.children().remove();
            np.find(".serch-content").children().appendTo($(".serch-content"));
        }
    })
}

function getAmount() {
    let v = $("#initAmount").val()
    if (v == null) { v = 0; }
    return v;
}
function getCategoryID() {
    let v = $("#initCategoryID").val()
    if (v == null) { v = 0; }
    return v;
}
function getCityID() {
    let v = $("#initCityID").val()
    if (v == null) { v = 0; }
    return v;
}
function getParentCategoryID() {
    let v = $("#parentCategoryID").val()
    if (v == null) { v = 0; }
    return v;
}

function getCurrentPage() {
    let v = $("#currentPage").val()
    if (v == null) { v = 0; }
    return v;
}